const http = require('http');
const queryString = require('querystring');

// const server = http.createServer((req, res) => {
//     console.log('method:',req.method);
//     const url = req.url;
//     console.log('url:',req.url);
//     req.query = queryString.parse(url.split('?')[1]);
//     console.log('query: ', req.query)
//     res.end(
//         JSON.stringify(req.query)
//     );
// });
const server = http.createServer((req, res) => {
    const method = req.method;
    const url = req.url;
    const path = url.split('?')[0];
    const query = queryString.parse(url.split('?')[1]);


    // 设置返回格式为JSON
    res.setHeader('Content-type','application/json');

    // 定义返回数据
    const resData = {
        method,
        url,
        path,
        query
    }
    
    if(method === 'GET'){
        // res.end(JSON.stringify(resData));
        res.end(JSON.stringify(resData));
    }
    if (method === 'POST') {
        let postData = '';
        req.on('data', chunk => {
            postData = chunk.toString();
        })
        req.on('end', () => {
            resData.postData = postData;
            res.end(JSON.stringify(resData));
        })
    }
})


server.listen(8000)
console.log('ok')